package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.Indicator;
import cn.com.thinvent.dispatch.entity.IndicatorConfig;
import cn.com.thinvent.dispatch.entity.IndicatorResult;
import cn.com.thinvent.dispatch.entity.ModelTask;
import cn.com.thinvent.dispatch.entity.request.TaskDTO;
import cn.com.thinvent.dispatch.entity.response.ModelTaskResponse;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.IndicatorConfigService;
import cn.com.thinvent.dispatch.service.biz.IndicatorService;
import cn.com.thinvent.dispatch.web.utils.UserContext;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Api(description = "IndicatorConfig模块")
@Controller
@RequestMapping("/indicatorConfig")
@CrossOrigin(origins = "*")
@ResponseBody
public class IndicatorConfigController {
    @Autowired
    private IndicatorConfigService indicatorConfigService;
    /**
     * 分页查询任务列表
     * @param fileName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ReturnModel list(@RequestParam(value = "fileName", required = false) String fileName,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        PagedGridResult<IndicatorConfig> list = indicatorConfigService.list(UserContext.get(), fileName, pageNum, pageSize);
        return new ReturnModel(200, list, "");
    }
    /**
     * 新增或修改
     * @param indicatorConfig
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ReturnModel saveOrUpdate(@RequestBody IndicatorConfig indicatorConfig) {

        IndicatorConfig result = indicatorConfigService.saveOrUpdate(indicatorConfig);
        return new ReturnModel(200, result, "");

    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ReturnModel delete(@RequestParam("id") Integer id) {
        Integer delete = indicatorConfigService.delete(id);
        return new ReturnModel(200, delete, "");
    }

    /**
     * 立即执行
     * @param id
     * @return
     */
    @GetMapping("/actionNow")
    @ResponseBody
    public ReturnModel actionNow(@RequestParam("id") Integer id) {
        indicatorConfigService.actionNow(id, UserContext.get());
        return new ReturnModel(200, "", "");
    }
    /**
     * 重新执行
     * @param id
     * @return
     */
    @GetMapping("/renewAction")
    @ResponseBody
    public ReturnModel renewActionNow(@RequestParam("id") Integer id) {
        indicatorConfigService.renewAction(id, UserContext.get());
        return new ReturnModel(200, "", "");
    }
    /**
     * 查看结果
     * @param id
     * @return
     */
    @GetMapping("/viewResult")
    @ResponseBody
    public ReturnModel viewResult(@RequestParam("id") Integer id) {
     IndicatorResult indicatorResult = indicatorConfigService.viewResult(id, UserContext.get());
        return new ReturnModel(200, indicatorResult, "");
    }
    @PostMapping("/chat")
    @ResponseBody
    public ReturnModel aIChat(@RequestBody String prompt) {
        String res =  indicatorConfigService.aIChat(prompt);
        return new ReturnModel(200, res, "");
    }
    @PostMapping("/chatTest")
    @ResponseBody
    public ReturnModel aIChatTest(@RequestBody String prompt) {
        String res =  indicatorConfigService.aIChatTest(prompt);
        return new ReturnModel(200, res, "");
    }
}
